<?php

namespace App\Http\Controllers\Shop\Product;

use App\Http\Controllers\ShopController;
use App\Models\Business\Address as AddressModel;
use App\Models\Product\Product as ProductModel;
use App\Models\Product\Cart as CartModel;
use Illuminate\Support\Facades\Validator;

class CartController extends ShopController
{
    public function index()
    {
        $busid = request('busid',0);
        $page = request('page',1);
        $limit = request('limit',10);
        
        $start = ($page - 1) * $limit;

        $CartData = CartModel::with(['product'])->where(['busid' => $busid])->offset($start)->limit($limit)->get();
        $count = CartModel::with(['product'])->where(['busid' => $busid])->count();

        if($CartData)
        {
            return $this->success('查询成功',['list' => $CartData,'count' => $count]);
        }else{
            return $this->error('暂无购物车数据');
        }
    }

    public function add()
    {
        $busid = request('busid',0);
        $proid = request('proid',0);

        $product = ProductModel::find($proid);

        if(!$product)
        {
            return $this->error('商品不存在');
        }

        $cart = CartModel::where(['busid' => $busid,'proid' => $proid])->first();

        if($product['stock'] == 0)
        {
            return $this->error('库存不足');
        }

        if($cart)
        {
            // 购物车的商品数量
            $ProductNumber = $cart['nums'] + 1;

            $cart->nums = $ProductNumber;
            $cart->total = bcmul($ProductNumber,$cart['price']);

            $result = $cart->save();
        }else{
            // 从未加购物车
            $data = [
                'busid' => $busid,
                'proid' => $proid,
                'nums' => 1,
                'price' => $product['price'],
                'total' => bcmul(1,$product['price'],2)
            ];

            $validate = [
                [
                    'busid' => 'required', //必填
                    'proid' => 'required', //必填
                    'nums' => 'required', //必填
                    'price' => 'required', //必填
                    'total' => 'required', //必填
                ],
                [
                    'busid.required'  => '用户未知',
                    'proid.required'  => '商品未知',
                    'nums.required'  => '请选择商品数量',
                    'price.required'  => '请输入商品的单价',
                    'total.required'  => '请输入商品总价',
                ]
            ];
    
            $validator = Validator::make($data,...$validate);
    
            if($validator->fails())
            {
                return $this->error($validator->errors()->first());
            }

            $result = CartModel::create($data);
        }

        if($result === false)
        {
            return $this->error('加入购物车失败');
        }else{
            return $this->success('加入购物车成功');
        }
    }

    public function update()
    {
        $busid = request('busid',0);
        $id = request('id',0);
        $nums = request('nums',0);

        $cart = CartModel::where(['busid' => $busid,'id' => $id])->first();

        if(!$cart)
        {
            return $this->error('该购物车不存在');
        }

        $product = ProductModel::find($cart['proid']);

        if(!$product)
        {
            return $this->error('商品不存在');
        }

        if($nums > $product['stock'])
        {
            return $this->error('库存不足');
        }

        $cart->nums = $nums;
        $cart->total = bcmul($nums,$cart['price'],2);

        $result = $cart->save();

        if($result === false)
        {
            return $this->error('更新商品数量失败');
        }else{
            return $this->success('更新商品数量成功');
        }
    }

    public function info()
    {
        $cartidsStr = request('cartids','');

        $cartids = explode(',',$cartidsStr);

        $cartids = array_filter($cartids);

        $cartData = CartModel::with(['product'])->whereIN('id',$cartids)->get();

        if(count($cartData) > 0)
        {
            return $this->success('查询成功',$cartData);
        }else{
            return $this->error('购物车不存在');
        }
    }

    // 获取用户默认收货地址
    public function default()
    {
        $busid = request('busid',0);

        $address = AddressModel::where(['busid' => $busid,'status' => (String)1])->first();

        if($address)
        {
            return $this->success('查询成功',$address);
        }else{
            return $this->error('获取不到默认收货地址');
        }
    }
}
